(function($) {
	var debug = function(args) {
		console.log(args);
	};
	var LuckyDraw = function(container, config) {
		var opt = {
			server: "server.php",
			fps: 35,
			size: 230,
			segments: [],
			speed: Math.PI / 16,
			uptime: 50,
			downtime: 300,
			centerX: 250,
			centerY: 250,
			width: 500,
			height: 500,
			stopable: 0,
			stopminarc: 0,
			stopmaxarc: 0,
			segarc: 0,
			segarc8: 0
		};
		$.extend(opt, config);
		if (!opt.segments.length) {
			return;
		}
		this.vars = opt;
		this.vars.container = $(container);
		this.initialize();
	};
	LuckyDraw.prototype = {
		initialize: function() {
			try {
				this.initWheel();
				this.initCanvas();
				this.update();
			} catch (e) {
				debug("Wheel is not loaded " + e);
			}
			try {
				this.initAudio();
			} catch (e) {
				debug("Wheel is not loaded " + e);
			}
		},
		initAudio: function() {
			return true;
		},
		initCanvas: function() {
			var canvas = this.vars.container.find("canvas").get(0);
			if (typeof FlashCanvas != "undefined") {
				FlashCanvas.initElement(canvas);
			}
			this.ctx = canvas.getContext("2d");
			$(this.vars.trigger).on("click touchstart", $.proxy(this.spin, this));
		},
		initWheel: function() {
			var n = this.vars, i, t;
			for (this.PI2 = Math.PI * 2, this.segarc = this.PI2 / n.segments.length,
					this.segarc8 = this.segarc / 8, this.stopable = 0,
					this.playing = false, i = 0, t = 0; t < n.downtime; t++) {
				i += n.speed * t / n.downtime;
			}
			this.stoparclength = i;
		},
		play: function(index) {
			if (this.playing) {
				return false;
			}
			var self = this;
			this.stopminarc = 0;
			this.stopmaxarc = 0;
			this.stopable = 0;
			this.frames = 0;
			this.phrase = 0;
			this.playing = true;
			this.sound && this.sound.play();
			this.stimer = setInterval($.proxy(this.onTimerTick, this), this.vars.fps);

			setTimeout(function() {
				self.stop(index);
			}, self.vars.uptime * self.vars.fps + 1e3);
			return true;
		},
		stop: function(n) {
			var i = this.segarc * parseInt(n),
					t = this.PI2 - (this.stoparclength + i) % this.PI2;
			this.stopminarc = t + this.segarc8;
			this.stopmaxarc = t + this.segarc - this.segarc8;
			this.stopable = 1;
		},
		onTimerTick: function() {
			var e = this.vars,
					t = false, i;
			for (this.draw(), this.phrase == 0 ? (this.frames++,
					this.adelta = this.vars.speed * (this.frames / this.vars.uptime),
					this.frames >= this.vars.uptime && (this.phrase = 1)) : this.phrase == 1 ? (this.adelta = this.vars.speed, this.cc = 0) : this.phrase == 2 && (
					this.frames++, this.adelta = this.vars.speed * ((this.vars.downtime - this.frames) / this.vars.downtime),
					this.frames >= this.vars.downtime && (t = true)), this.cangle += this.adelta; this.cangle >= this.PI2; ) {
				this.cangle -= this.PI2;
			}
			this.phrase == 1 && this.stopable == 1 && this.cangle >= this.stopminarc &&
					this.cangle <= this.stopmaxarc && (this.frames = 0, this.phrase = 2),
					t && (clearInterval(this.stimer), this.stimer = 0, this.adelta = 0,
					e.onfinish && (i = this, setTimeout(function() {
				e.onfinish(i);
			}, 100)), this.playing = false);
		},
		update: function() {
			this.cangle = -.5 / this.vars.segments.length * this.PI2;
			this.draw();
		},
		draw: function() {
			this.clear();
			this.drawWheel();
		},
		clear: function() {
			this.ctx.clearRect(0, 0, this.vars.width, this.vars.height);
		},
		drawSegment: function(n, t, i) {
			var r = this.ctx,
					f = this.vars.segments[n];
			var color = this.vars.colors[n];
			r.save();
			r.beginPath();
			r.fillStyle = color;
			r.strokeStyle = color;
			r.moveTo(this.vars.centerX, this.vars.centerY);
			r.arc(this.vars.centerX, this.vars.centerY, this.vars.size, t, i, false);
			r.lineTo(this.vars.centerX, this.vars.centerY);
			r.closePath();
			r.lineWidth = 1;
			r.fill();
			r.restore();
			r.save();
			r.translate(this.vars.centerX, this.vars.centerY);
			r.rotate((t + i) / 2);
			r.strokeStyle = "#ffffff";
			r.fillText(f.text, this.vars.size / 2 + 30, 0);
			r.restore();
		},
		drawWheel: function() {
			var e = this.vars,
					n = this.ctx,
					r = this.cangle,
					u = r,
					o = e.segments,
					f = o.length,
					t, i;
			for (n.textBaseline = "middle", n.textAlign = "center", n.font = "bold 12px verdana",
					n.strokeStyle = "rgba(220, 220, 220, 0.6)", n.lineWidth = 1, t = 1; t <= f; t++) {
				i = this.PI2 * (t / f) + r;
				this.drawSegment(t - 1, u, i);
				u = i;
			}
		}
	};
	$.fn.luckydraw = function(i) {
		return this.each(function() {
			var u = $(this), r = u.data("luckydraw"), f = typeof i == "object" && i;
			r ? typeof i == "string" && r[i] && r[i]() : u.data("luckydraw", r = new LuckyDraw(this, f));
		});
	}
})(jQuery);